package com.fly.msg;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

import static com.fly.msg.Constant.*;

/**
 * @author frinder
 * @date 2018/6/1
 * @className WebSocketConfig
 * @package com.fly.msg
 * @Description: WebSocket配置
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        // 注册 stomp 切点
        stompEndpointRegistry.addEndpoint(STOMP_ENDPOINT).setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 服务端发送消息域
        registry.enableSimpleBroker(TOPIC, P2P_ADDRESS_PREFIX);
        // 点对点消息前缀
        registry.setUserDestinationPrefix(P2P_ADDRESS_PREFIX);
        // 客户端向服务端发送消息前缀
        registry.setApplicationDestinationPrefixes(WS_CLIENT_CALL_PREFIX);
    }
}
